Skip to content

fix: clear selection on undo/redo#2385

Open
VladaHarbour wants to merge 2 commits intomainfrom
sd-2178_history-selection
Open

fix: clear selection on undo/redo#2385
VladaHarbour wants to merge 2 commits intomainfrom
sd-2178_history-selection

Conversation

@VladaHarbour
Copy link
Contributor

No description provided.

@VladaHarbour VladaHarbour self-assigned this Mar 13, 2026
@linear
Copy link

linear bot commented Mar 13, 2026

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f12577e085

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Contributor

@caio-pizzol caio-pizzol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladaHarbour good approach — wrapping undo/redo to clear the ghost highlight makes sense.

main question: the fix also collapses the cursor after every undo/redo, not just the ghost highlight case. could it work without that part? also, the collab undo path skips the fix entirely. left inline comments with details.

on DX: one object literal duplicates DEFAULT_SELECTION_STATE (drift risk if the shape changes), and a dead typeof guard can be simplified — both minor.

test coverage: redo path uses the same wrapper but has no test, and the new setOptions call in the non-toolbar blur branch isn't covered.

@@ -55,7 +88,8 @@ export const History = Extension.create({
return yUndo(state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collab undo (yUndo) returns before this cleanup runs, so the ghost highlight bug would still happen in collab sessions. does it need to be handled there too?

});

const sel = cleared.selection;
if (sel && sel instanceof TextSelection && !sel.empty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this removes the text selection after every undo/redo. most editors keep the restored text selected so users see what changed. is that ok here, or could we skip this part and just clear the highlight state (steps 1 and 3)?

if (!dispatch) return dispatch;

return (historyTr) => {
let cleared = historyTr.setMeta(CustomSelectionPluginKey, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this object is the same as DEFAULT_SELECTION_STATE in custom-selection.js. if one changes and the other doesn't, they'll get out of sync. worth importing it instead?


const sel = cleared.selection;
if (sel && sel instanceof TextSelection && !sel.empty) {
const headPos = typeof sel.head === 'number' ? sel.head : sel.to;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after the instanceof TextSelection check above, sel.head is always a number, so the typeof check and sel.to fallback never run. can simplify to just TextSelection.create(cleared.doc, sel.head).

// Also clear editor-level preserved selection snapshots so that
// subsequent commands (linked styles, mark commands, etc.) don't
// resurrect an old selection after history undo/redo.
this.editor.setOptions({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new clearing code doesn't have a test. the existing blur tests don't hit this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants